UART.c

					
/*
 * Created: 30.01.2022
 *  Author: Bohdan
 */ 

#include "avr/io.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "UART_avr.h"

/* UART Initialization. 
 * So far made only for UART0
 */
void UART0_start(unsigned long baud) 
{
	uint16_t ubrr = ((F_CPU/16/baud)-1);
	
	//Set baud rate
	UBRR0 = ubrr; //(unsigned char)(ubrr>>8);
	//UBRR0L = (unsigned char)ubrr;
	// Enable receiver and transmitter
	UCSR0B = 0;
	UCSR0B |=  (1<<RXCIE0) | (1<<RXEN0) | (1<<TXEN0);   // RXCIE0 - Interrupts after receive are allowed
	
	//UCSR0C &= ~((1<<UMSEL00)|(1<<UMSEL01));			// Asynchronous UART - 00
	//UCSR0C &= ~(1<<UCSZ02);
	UCSR0C = 0;
	UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00);				// Bits UCSZ0х = 0b011 - Symbol size - 8 bit
	UDR0 = 0;										// Flushing the Data-Register
	//UCSR0C |= (1<<USBS0);							// 2 Stop Bits; 0 - of this bit gives 1 Stop Bit
}


/*****************************************************************************************************************/
void UART_TR( char data ) // Sending a byte per UART
{   // Wait for empty transmit buffer
	while ( !( UCSR0A & (1<<UDRE0)) ); 
	// Put data into buffer, sends the data
	UDR0 = data;       
}

/*void UART_Transmit_int( unsigned long data )
{   
	char number[12];
	ultoa(data, &number, 10);
	UART_Transmit_str(&number); 
}*/


uint8_t UART_REC(void)
{   /* Wait for data to be received */
	while ( !(UCSR0A & (1<<RXC0)) );
	/* Get and return received data from buffer */
	return UDR0;
}

void print_str(char *str)
{
	uint16_t i = 0;
	
	while(str[i] != '\0')
	{
		UART_TR(str[i]);	//printing symbol
		i++;
	}
	UDR0 = 0;
}